<?php
// $Id$

/**
 * Controller_Botag 控制器
 */
class Controller_BoTag extends Controller_Boprepare
{
    /**
     * 执行流程:
     * 任务描述：将新闻标签写入表
     * {表 Tags}
     * 记录格式 (tag_id)       1
     *          (category)     newstoce
     *          (tag)          美国
     *          (counts)       15
     *          (isvisible)    1
     *          (isonhomepage) 0
     *          (hit as query) 179
     *
     * {表 News}
     * 更新：新闻中tag 字段的记录格式: #新加坡#,#美国#,#英国#
     */
    public function actionInitiateTag()
    {

        $Pinittags = isset($_POST['inittags']) ? 1 : 0;

        $notice = '准备执行初始化';

        if($Pinittags == 1)
        {
            // 执行一次 TRUNCATE TABLE 语句
            QDB::getConn()->execute('TRUNCATE TABLE tag');

            // 新闻通知版块是 ARRAY_MERGE getSubArray(4,7,8,23)
            $newsBlockCtgry = NewsCategory::getSelfArrayId(array(4,7,8,23));
            $newsBlockTags  = News::find('tag != ? AND category_id in (?)', '', $newsBlockCtgry)->all()->query()->values('tag');
            $newsBlockTagCounterArray = array();
            foreach($newsBlockTags as $newsBlockTag)
            {
                $newsBlockTagCounterArray = array_merge($newsBlockTagCounterArray, explode(',', $newsBlockTag));
            }
            $newsBlockTagValues = array_count_values($newsBlockTagCounterArray);
            arsort($newsBlockTagValues);

            // 国际交流版块是 getSubArray(81)
            $internationallyBlockCtgry = NewsCategory::getSelfArrayId(81);
            $internationallyBlockTags  = News::find('tag != ? AND category_id in (?)', '', $internationallyBlockCtgry)->all()->query()->values('tag');
            $internationallyBlockTagCounterArray = array();
            foreach($internationallyBlockTags as $internationallyBlockTag)
            {
                $internationallyBlockTagCounterArray = array_merge($internationallyBlockTagCounterArray, explode(',', $internationallyBlockTag));
            }
            $internationallyBlockTagValues = array_count_values($internationallyBlockTagCounterArray);
            arsort($internationallyBlockTagValues);

            // 创新实践版块是 getSubArray(83)
            $innovationBlockCtgry = NewsCategory::getSelfArrayId(83);
            $innovationBlockTags  = News::find('tag != ? AND category_id in (?)', '', $innovationBlockCtgry)->all()->query()->values('tag');
            $innovationBlockTagCounterArray = array();
            foreach($innovationBlockTags as $innovationBlockTag)
            {
                $innovationBlockTagCounterArray = array_merge($innovationBlockTagCounterArray, explode(',', $innovationBlockTag));
            }
            $innovationBlockTagValues = array_count_values($innovationBlockTagCounterArray);
            arsort($innovationBlockTagValues);

            /**
              * 输入数据库
             * $newsBlockTagValues
             * $internationallyBlockTagValues
             * $innovationBlockTagValues
             */
            foreach($newsBlockTagValues as $k => $v)
            {
                if($k == '')
                {
                    continue;
                }

                $obj = new Tag(
                    array(
                        'category'     => '新闻通知',
                        'tag'          => $k,
                        'counts'       => $v,
                        'isvisible'    => 1,
                        'isonhomepage' => 0,
                        'hit_as_query' => 0,
                ));
                $obj->save();
            }
            foreach($internationallyBlockTagValues as $k => $v)
            {
                if($k == '')
                {
                    continue;
                }

                $obj = new Tag(
                    array(
                        'category'     => '国际交流',
                        'tag'          => $k,
                        'counts'       => $v,
                        'isvisible'    => 1,
                        'isonhomepage' => 0,
                        'hit_as_query' => 0,
                ));
                $obj->save();
            }
            foreach($innovationBlockTagValues as $k => $v)
            {
                if($k == '')
                {
                    continue;
                }

                $obj = new Tag(
                    array(
                        'category'     => '创新实践',
                        'tag'          => $k,
                        'counts'       => $v,
                        'isvisible'    => 1,
                        'isonhomepage' => 0,
                        'hit_as_query' => 0,
                ));
                $obj->save();
            }

            $notice = '初始化执行完毕';
        }

        $this->_view['notice'] = $notice;
    }

    /**
     * 兼容旧版本标签
     *
     * 例： 公共关系,创业   -->  ,公共关系,创业,
     */
    public function actionCompatifyTag()
    {
        $news = News::find('tag != ?', '')->all()->query();

        foreach($news as $n)
        {
            $isChanged = 0;

            if(strpos($n->tag, ',') + 1 !== 1)
            {
                $isChanged = 1;
                $n->tag = ','.$n->tag;
            }

            if($n->tag{mb_strlen($n->tag)-1} != ',')
            {
                $isChanged = 1;
                $n->tag .= ',';
            }

            if($isChanged == 1)
            {
                $n->save();
            }
        }

        echo '执行完毕！';
        return $this->_forward('bohome/index');
    }

    /**
     *
     * 首页与新闻通知的标签
     */
    public function actionRemapNewsNoticeTag()
    {
        $Pisonhomepage = isset($_POST['isonhomepage']) ? $_POST['isonhomepage'] : NULL;
        $Pisvisible    = isset($_POST['isvisible'])    ? $_POST['isvisible']    : NULL;

        $notice = '可控标签管理';
        if($Pisonhomepage || $Pisvisible)
        {
            $onewsBlockTags = Tag::find('category = ?', '新闻通知')->all()->query();
            foreach($onewsBlockTags as $tag)
            {
                $tag->isonhomepage = 0;
                $tag->isvisible    = 0;
                $tag->save();
            }

            $newsBlockTagsOnHomepage = Tag::find('tag_id in (?)', $Pisonhomepage)->all()->query();
            foreach($newsBlockTagsOnHomepage as $tag)
            {
                $tag->isonhomepage = 1;
                $tag->save();
            }

            $newsBlockTagsVisible = Tag::find('tag_id in (?)', $Pisvisible)->all()->query();
            foreach($newsBlockTagsVisible as $tag)
            {
                $tag->isvisible = 1;
                $tag->save();
            }

            $notice = '标签已修改!';
        }

        $this->_view['notice'] = $notice;
    }
    /**
     *
     * 国际交流的标签
     */
    public function actionRemapInternationallyTag()
    {
        $Pisonhomepage = isset($_POST['isonhomepage']) ? $_POST['isonhomepage'] : NULL;
        $Pisvisible    = isset($_POST['isvisible'])    ? $_POST['isvisible']    : NULL;

        $notice = '可控标签管理';
        if($Pisonhomepage || $Pisvisible)
        {
            $ointernationallyBlockTags = Tag::find('category = ?', '国际交流')->all()->query();
            foreach($ointernationallyBlockTags as $tag)
            {
                $tag->isonhomepage = 0;
                $tag->isvisible    = 0;
                $tag->save();
            }

            $internationallyBlockTagsOnHomepage = Tag::find('tag_id in (?)', $Pisonhomepage)->all()->query();
            foreach($internationallyBlockTagsOnHomepage as $tag)
            {
                $tag->isonhomepage = 1;
                $tag->save();
            }

            $internationallyBlockTagsVisible = Tag::find('tag_id in (?)', $Pisvisible)->all()->query();
            foreach($internationallyBlockTagsVisible as $tag)
            {
                $tag->isvisible = 1;
                $tag->save();
            }

            $notice = '标签已修改!';
        }

        $this->_view['notice'] = $notice;
    }
    /**
     *
     * 创新实践的标签
     */
    public function actionRemapInnovationTag()
    {
        $Pisonhomepage = isset($_POST['isonhomepage']) ? $_POST['isonhomepage'] : NULL;
        $Pisvisible    = isset($_POST['isvisible'])    ? $_POST['isvisible']    : NULL;

        $notice = '可控标签管理';

        if($Pisonhomepage || $Pisvisible)
        {
            $oinnovationBlockTags = Tag::find('category = ?', '创新实践')->all()->query();
            foreach($oinnovationBlockTags as $tag)
            {
                $tag->isonhomepage = 0;
                $tag->isvisible    = 0;
                $tag->save();
            }

            $innovationBlockTagsOnHomepage = Tag::find('tag_id in (?)', $Pisonhomepage)->all()->query();
            foreach($innovationBlockTagsOnHomepage as $tag)
            {
                $tag->isonhomepage = 1;
                $tag->save();
            }

            $innovationBlockTagsVisible = Tag::find('tag_id in (?)', $Pisvisible)->all()->query();
            foreach($innovationBlockTagsVisible as $tag)
            {
                $tag->isvisible = 1;
                $tag->save();
            }

            $notice = '标签已修改!';
        }

        $this->_view['notice'] = $notice;
    }
}